<template>
  <div class="consultation">
    <h2>{{ title }}</h2>
    <div class="operation">
      <el-button size="mini" type="primary" @click="addRow">添加行</el-button>
    </div>
    <div class="consultation-block">
      <div class="table">
        <el-table
          size="mini"
          :data="tableData"
          highlight-current-row
          style="width: 100%"
        >
          <el-table-column
            :label="col"
            :prop="index + ''"
            v-for="(col, index) in columns"
          >
          </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button
                type="danger"
                size="mini"
                @click="deleteData(scope)"
                >删除</el-button
              >
              <el-button
                type="warning"
                size="mini"
                @click="updateData(scope)"
                >修改</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="isVisible"
      width="50%"
      :before-close="handleClose"
    >
      <div v-for="(form, index) in ruleForm">
        <div style="margin-bottom: 4px;">{{ form.label }}</div>
        <el-input
          style="margin-bottom: 8px;"
          type="text"
          v-model="form.value"
        ></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserInfo, updateUserData } from "../service.js";

export default {
  data() {
    return {
      tableData: [],
      columns: [],
      dialogTitle: "",
      isVisible: false,
      ruleForm: [],
      title: "",
      rowIndex: ''
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    addRow() {
      this.dialogTitle = "新增数据";
      this.isVisible = true;
      for (let item of this.ruleForm) {
        item.value = "";
      }
    },
    getData() {
      this.ruleForm = []
      getUserInfo(JSON.parse(localStorage.getItem("userInfo"))._id).then(
        (res) => {
          this.columns = JSON.parse(res.data.column);
          this.title = res.data.title;
          for (let i = 1; i <= this.columns.length; i++) {
            this.ruleForm.push({
              label: this.columns[i - 1],
              value: "",
            });
          }
          this.tableData = JSON.parse(res.data.data);
          console.log(this.tableData);
        }
      );
    },
    handleClose() {
      this.isVisible = false;
    },
    submitForm() {
      let obj = {};
      for (let i = 0; i < this.ruleForm.length; i++) {
        obj[i] = this.ruleForm[i].value;
      }
      if (this.dialogTitle == "新增数据") {
        this.tableData.push(obj);
      } else {
        this.tableData[this.rowIndex] = obj
      }
      console.log(this.tableData)
      // 保存表格数据-修改用户信息
      this.handle()
    },
    deleteData(row) {
      console.log(row.$index)
      this.tableData.splice(row.$index, 1)
      this.handle()
    },
    updateData(row) {
      console.log(this.ruleForm)
      this.dialogTitle = "编辑数据";
      this.isVisible = true;
      let rowData = this.tableData[row.$index]
      console.log(Object.values(rowData))
      this.rowIndex = row.$index
      this.ruleForm.forEach((v, i) => {
        this.ruleForm[i].value = Object.values(rowData)[i]
      })
    },
    handle() {
      updateUserData(JSON.parse(localStorage.getItem("userInfo"))._id, {
        data: JSON.stringify(this.tableData),
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success("添加成功");
          this.isVisible = false;
          this.getData()
        }
      });
    }
  },
};
</script>

<style scoped>
h2 {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.consultation-block {
  width: 90%;
  margin-left: 5%;
}
.pagination {
  text-align: center;
  margin-top: 20px;
}
.operation {
  margin-left: 5%;
  margin-bottom: 20px;
}
</style>
